<?php

namespace App\Admin\Controllers;

use App\Models\Teacher;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class TeacherController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.teacher'));
            $content->description(trans('admin.list'));
            
            $content->body($this->grid()->render());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.teacher'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.teacher'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Teacher::class, function (Grid $grid) {
            $grid->id(trans('admin.ID'))->sortable();
            $grid->ch_name(trans('admin.ch_name'))->sortable();
            $grid->en_name(trans('admin.en_name'))->sortable();
            $grid->sex(trans('admin.sex'))->display(function ($sex) {
                if($this->sex==1){
                    return trans('admin.men');
                }elseif ($this->sex==2) {
                    return trans('admin.women');
                }else{
                    return 'N/A';
                }
            })->sortable();
            $grid->tel(trans('admin.tel'))->sortable();
            $grid->email(trans('admin.email'))->sortable();

            //禁用导出按钮
            $grid->disableExport();
            
            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });

            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->like('ch_name',trans('admin.ch_name'));
                });
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->like('en_name',trans('admin.en_name'));
                });
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Teacher::class, function (Form $form) {
            $form->display('id', 'ID');

            $form->text('ch_name', trans('admin.ch_name'))->rules('required')->addElementLabelClass('must-input');
            $form->text('en_name', trans('admin.en_name'))->rules('required')->addElementLabelClass('must-input');
            $form->select('sex', trans('admin.sex'))->options([1 => trans('admin.men'), 2 => trans('admin.women')]);
            $form->email('email', trans('admin.email'));
            $form->mobile('tel', trans('admin.tel'))->options(['mask' => '999 9999 9999']);
            //去掉添加/编辑表单的重置按钮
            $form->disableReset();
        });
    }
}
